#include <iostream>
#include <SDL/SDL_image.h>
#include "Effect.h"
#include "Sprite.h"

using namespace std;
using namespace JDL;

Sprite::Sprite(const string file)
{
  _surface = IMG_Load(file.c_str());
  if (_surface != NULL)
    {
        this->InitRect(&_clipper);
	this->InitRect(&_pos);
	this->InitRect(&_old_pos);
	this->InitRect(&_pos_c);
	this->InitRect(&_zone);
	_zone.w = _surface->w;
	_zone.h = _surface->h;
    }
  else
    fprintf(stderr, "Error %s\n", SDL_GetError());
}

Sprite::~Sprite()
{
  SDL_FreeSurface(_surface);
}

void	Sprite::Render(SDL_Surface *screen)
{
  Effect	*eff;
  std::map<string, Effect*>::iterator	it;

  _pos_c.x = _pos.x;
  _pos_c.y = _pos.y;
  SDL_BlitSurface(_surface, &_zone, screen, &_pos_c);
  for (it = _effect.begin(); it != _effect.end(); it++)
    {
      eff = it->second;
      eff->Do(this);
    }
  SDL_SetClipRect(screen, NULL);
}
